package com.starleague.battletech.unit.mech.jpa.appengine;

import com.starleague.battletech.BattletechException;
import com.starleague.battletech.unit.IWeight;
import com.starleague.battletech.unit.mech.AbstractMechGyroFactory;
import com.starleague.battletech.unit.mech.IMutableMech;
import com.starleague.battletech.unit.mech.MechGyroType;
import com.starleague.battletech.unit.mech.MechHitLocationPosition;

public class MechGyroGaeFactory
        extends AbstractMechGyroFactory
{
    @Override
    protected MechGyroGae addGyro(IMutableMech mech,
                                  IWeight gyroWeight,
                                  MechGyroType gyroType) throws BattletechException
    {
        MechGyroGae gyro = new MechGyroGae((MechGae) mech, gyroWeight, gyroType);

        MechCriticalSlotGae[] centerTorsoCriticalSlots = ((MechGae) mech).getCriticalSlotsFor(MechHitLocationPosition.CENTER_TORSO);
        int[] requiredCenterTorsoSlotIndices = { 3, 4, 5, 6 };
        for (int requiredCenterTorsoSlotIndex : requiredCenterTorsoSlotIndices)
        {
            if (centerTorsoCriticalSlots[requiredCenterTorsoSlotIndex] != null)
                throw new BattletechException("CenterTorso should have a free slot at ["
                                              + requiredCenterTorsoSlotIndex
                                              + "]: "
                                              + centerTorsoCriticalSlots[requiredCenterTorsoSlotIndex]);

            gyro.getCriticalSlots().add(new MechGyroCriticalSlotGae(gyro,
                                                                    MechHitLocationPosition.CENTER_TORSO,
                                                                    requiredCenterTorsoSlotIndex));
        }

        ((MechGae) mech).setGyro(gyro);
        return gyro;
    }
}
